package com.fyzk.activity.lottery.zc.tz_dialog;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.tyjc.lottery.R;
import com.fyzk.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class JZ_HG_Pop extends PopupWindow {

    private LinearLayout ll_bf1, ll_bf2, ll_bf3, ll_bf4, ll_bf5,
            ll_zjq1, ll_zjq2, ll_bqc1, ll_bqc2, ll_bqc3;
    //顶部
    private TextView zhu, ke;
    //底部
    private TextView cancel, sure;
    protected JCInfo infor;

    protected Context context;
    private LinearLayout ll_bqc;
    private TextView txt_bqc_stop;


    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            addViews();
        }
    };

    private String[] keyArray;


    public JZ_HG_Pop(Context context, int width, int height, JCInfo infos, int changCiNum) {
        super(width, height);
        this.context = context;
        this.infor = infos;
        View view = View.inflate(context, R.layout.layout_jz_hh_pop, null);
        info(view);
        setContentView(view);
        setOutsideTouchable(true);
    }

    public void sureClick() {
        int moreSelCount = 0;
        int sf_sel = infor.getOnclikNum()-infor.getHg_more();
        for (ZCBF_infor info : zcbf_infors) {
            int index = zcbf_infors.indexOf(info);
            infor.getTouZhuInfor().get(index+6).setSelect(info.isSelect());
            if (info.isSelect()) {
                moreSelCount++;
            }
        }

        int index = 0;
        for (int i = 6; i < keyArray.length; i++) {
            infor.btnHGPojo.get(keyArray[i]).isSel = zcsp_infors.get(index).isSel;
            index++;
        }
        infor.setHg_more(moreSelCount);
        infor.setOnclikNum(sf_sel+moreSelCount);
        infor.setResult(getResult(infor));
    }

    protected JCInfo getResult() {
        return infor;
    }


    List<ZCBF_infor> zcbf_infors = new ArrayList<>();
    List<JCInfo.SPInfo> zcsp_infors = new ArrayList<>();

    /**
     * 添加投注区 小模块
     *
     * @param position
     * @param str1     名称（如：让球胜）
     * @param str2     赔率（如：1.20）
     * @param weight   布局权重（所在行）
     * @return
     */
    private View addTextView(int position, String str1, String str2, float weight, int orientationmode) {
        View view1 = View.inflate(context, R.layout.zc_dialog_hh_tv, null);
        final LinearLayout ll = view1
                .findViewById(R.id.zc_dlg_hh_ll);
        final TextView tv1 = view1.findViewById(R.id.zc_dlg_hh_tv1);
        final TextView tv2 = view1.findViewById(R.id.zc_dlg_hh_tv2);
        ll.setOrientation(orientationmode);
        tv1.setText("" + str1);
        tv2.setText("" + str2);
        LinearLayout.LayoutParams pl = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, weight);

        if (infor.getTouZhuInfor().get(position).isSelect()) {
            ll.setBackgroundColor(context.getResources().getColor(
                    R.color.red));
            tv1.setTextColor(Color.WHITE);
            tv2.setTextColor(Color.WHITE);
        } else {
            ll.setBackgroundColor(Color.WHITE);
            tv1.setTextColor(Color.BLACK);
            tv2.setTextColor(context.getResources().getColor(
                    R.color.zc_dlg_hh_tv2));
        }

        ZCBF_infor zcbf_infor = null;
        JCInfo.SPInfo spInfo = null;
        try {
            zcbf_infor = infor.getTouZhuInfor().get(position).clone();
            spInfo = infor.btnHGPojo.get(keyArray[position]).clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        zcbf_infors.add(zcbf_infor);
        zcsp_infors.add(spInfo);
        ll.setTag(R.string.bfinfo, zcbf_infor);
        ll.setTag(R.string.spinfo, spInfo);

        ll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ZCBF_infor bfInfo = (ZCBF_infor) v.getTag(R.string.bfinfo);
                JCInfo.SPInfo spInfo = (JCInfo.SPInfo) v.getTag(R.string.spinfo);
                boolean isSel = !bfInfo.isSelect();
                spInfo.isSel = isSel;
                bfInfo.setSelect(isSel);
                if (isSel) {
                    ll.setBackgroundColor(context.getResources().getColor(
                            R.color.red));
                    tv1.setTextColor(Color.WHITE);
                    tv2.setTextColor(Color.WHITE);
                } else {
                    ll.setBackgroundColor(Color.WHITE);
                    tv1.setTextColor(Color.BLACK);
                    tv2.setTextColor(context.getResources().getColor(
                            R.color.zc_dlg_hh_tv2));
                }

            }
        });
        pl.setMargins(Utils.dip2px(0.5f), 0, 0, 0);
        view1.setLayoutParams(pl);
        return view1;
    }

    private void addView1() {
        if (infor.getTouZhuInfor() == null
                || infor.getTouZhuInfor().size() == 0) {

            return;
        }
        new Thread(new Runnable() {

            @Override
            public void run() {

                handler.sendEmptyMessage(0);

            }
        }).start();

    }

    private void addViews() {
        for (int t = 6; t < infor.getTouZhuInfor().size(); t++) {
            ZCBF_infor touZhu = infor.getTouZhuInfor().get(t);
            if (t >= 6 && t < 13) {
                ll_bf1.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.VERTICAL));
            } else if (t >= 13 && t < 19) {
                if (t == 18) {
                    ll_bf2.addView(addTextView(t, touZhu.getBiFen(),
                            touZhu.getPeilv(), 3, LinearLayout.VERTICAL));
                } else {
                    ll_bf2.addView(addTextView(t, touZhu.getBiFen(),
                            touZhu.getPeilv(), (float) 3.6, LinearLayout.VERTICAL));
                }
            } else if (t >= 19 && t < 24) {
                if (t == 23) {
                    ll_bf3.addView(addTextView(t, touZhu.getBiFen(),
                            touZhu.getPeilv(), 2, LinearLayout.VERTICAL));
                } else {
                    ll_bf3.addView(addTextView(t, touZhu.getBiFen(),
                            touZhu.getPeilv(), 3, LinearLayout.VERTICAL));
                }
            } else if (t >= 24 && t < 31) {
                ll_bf4.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.VERTICAL));
            } else if (t >= 31 && t < 37) {
                if (t == 36) {
                    ll_bf5.addView(addTextView(t, touZhu.getBiFen(),
                            touZhu.getPeilv(), 1, LinearLayout.VERTICAL));
                } else {
                    ll_bf5.addView(addTextView(t, touZhu.getBiFen(),
                            touZhu.getPeilv(), (float) 1.2, LinearLayout.VERTICAL));
                }
            } else if (t >= 37 && t < 41) {
                ll_zjq1.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.HORIZONTAL));
            } else if (t >= 41 && t < 45) {
                ll_zjq2.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.HORIZONTAL));
            } else if (t >= 45 && t < 48) {
                ll_bqc1.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.HORIZONTAL));
            } else if (t >= 48 && t < 51) {
                ll_bqc2.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.HORIZONTAL));
            } else if (t >= 51 && t < 54) {
                ll_bqc3.addView(addTextView(t, touZhu.getBiFen(),
                        touZhu.getPeilv(), 1, LinearLayout.HORIZONTAL));
            }
        }
    }


    /**
     * 计算 从投注区返回结果
     *
     * @param infor
     * @return
     */
    protected String getResult(JCInfo infor) {
        String str = "";
        for (int i = 0; i < infor.getTouZhuInfor().size(); i++) {
            if (infor.getTouZhuInfor().get(i).isSelect()) {
                str += infor.getTouZhuInfor().get(i).getBiFen() + ",";
            }
        }
        if (!str.equals("")) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }


    private void info(View view) {
        zhu = view.findViewById(R.id.zcbf_dialog_zhu);
        ll_bqc = view.findViewById(R.id.jz_hg_bqc_layout);
        txt_bqc_stop = view.findViewById(R.id.jz_hg_bqc_stop);
        ke = view.findViewById(R.id.zcbf_dialog_ke);
        cancel = view.findViewById(R.id.zcbf_dialog_btm_cancel);
        sure = view.findViewById(R.id.zcbf_dialog_btm_sure);
        zhu.setText(infor.getHomeTeam());
        ke.setText(infor.getAwayTeam());

        cancel.setOnClickListener(new ZCBF_DLG_onClick());
        sure.setOnClickListener(new ZCBF_DLG_onClick());

        keyArray = new String[]{infor.VS_3, infor.VS_1, infor.VS_0,
                infor.LETVS_3, infor.LETVS_1, infor.LETVS_0,
                infor.BF_10, infor.BF_20, infor.BF_21,
                infor.BF_30, infor.BF_31, infor.BF_32,
                infor.BF_40, infor.BF_41, infor.BF_42,
                infor.BF_50, infor.BF_51, infor.BF_52,
                infor.BF_90, infor.BF_00, infor.BF_11,
                infor.BF_22, infor.BF_33, infor.BF_99,
                infor.BF_01, infor.BF_02, infor.BF_12, infor.BF_03,
                infor.BF_13, infor.BF_23, infor.BF_04,
                infor.BF_14, infor.BF_24, infor.BF_05,
                infor.BF_15, infor.BF_25, infor.BF_09,
                infor.SCORE_0, infor.SCORE_1, infor.SCORE_2, infor.SCORE_3,
                infor.SCORE_4, infor.SCORE_5, infor.SCORE_6, infor.SCORE_7,
                infor.BQC_33, infor.BQC_31, infor.BQC_30, infor.BQC_13,
                infor.BQC_11, infor.BQC_10, infor.BQC_03, infor.BQC_01,
                infor.BQC_00};

        ll_bf1 = view.findViewById(R.id.zc_dlg_hh_bf1);
        ll_bf2 = view.findViewById(R.id.zc_dlg_hh_bf2);
        ll_bf3 = view.findViewById(R.id.zc_dlg_hh_bf3);
        ll_bf4 = view.findViewById(R.id.zc_dlg_hh_bf4);
        ll_bf5 = view.findViewById(R.id.zc_dlg_hh_bf5);

        ll_zjq1 = view.findViewById(R.id.zc_dlg_hh_zjq1);
        ll_zjq2 = view.findViewById(R.id.zc_dlg_hh_zjq2);

        ll_bqc1 = view.findViewById(R.id.zc_dlg_hh_bqc1);
        ll_bqc2 = view.findViewById(R.id.zc_dlg_hh_bqc2);
        ll_bqc3 = view.findViewById(R.id.zc_dlg_hh_bqc3);

        if (infor.getStaticSaleBqcStatus() == 2) {
            ll_bqc.setVisibility(View.VISIBLE);
            txt_bqc_stop.setVisibility(View.GONE);
        } else {
            ll_bqc.setVisibility(View.GONE);
            txt_bqc_stop.setVisibility(View.VISIBLE);
        }

        addView1();

    }

    /**
     * 单击事件
     *
     * @author Administrator
     */
    class ZCBF_DLG_onClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zcbf_dialog_btm_cancel:
                    JZ_HG_Pop.this.dismiss();
                    break;
                case R.id.zcbf_dialog_btm_sure:
                    sureClick();
                    JZ_HG_Pop.this.dismiss();
                    break;
                default:
                    break;
            }
        }
    }

}
